package VIEWS;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JToolBar;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.JScrollPane;

import CONTROLLERS.DonThuocConTrollers;
import CONTROLLERS.UsersControllers;
import ModelEntityFW.DonThuocEntity;
import ModelEntityFW.UserEntitys;
import Ultils.Controls;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.ImageIcon;

public class DONTHUOC extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private static Connection m_objConnection;
	private static UserEntitys m_objSecurityContext;
	private DonThuocConTrollers objcontroller;
	private DefaultTableModel objDTM;
	private DonThuocEntity m_objEntity;
	private static boolean bFlag = false;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the frame.
	 */
	public DONTHUOC(Connection objConnection, UserEntitys objSecurity) {
		m_objConnection = objConnection;
		m_objSecurityContext = objSecurity;
		objcontroller = new DonThuocConTrollers(m_objConnection,
				m_objSecurityContext);
		m_objEntity = new DonThuocEntity();

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 681, 416);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JToolBar toolBar = new JToolBar();
		toolBar.setBounds(10, 0, 665, 48);
		contentPane.add(toolBar);

		JButton btnNewButton = new JButton("THÊM");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				m_objEntity.setbFlag(false);
				THEMSUADONTHUOC frm = new THEMSUADONTHUOC(m_objConnection,
						m_objSecurityContext, m_objEntity);
				frm.setVisible(true);
			}
		});
		btnNewButton.setIcon(new ImageIcon(DONTHUOC.class
				.getResource("/images/13.png")));
		toolBar.add(btnNewButton);
		btnNewButton.setFont(new Font("Cambria", Font.PLAIN, 14));

		JButton btnXa = new JButton("XÓA ");
		btnXa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (bFlag) {
					m_objEntity.setbFlag(bFlag);
					m_objEntity.setThuocID(Integer.parseInt(String
							.valueOf(objDTM.getValueAt(table.getSelectedRow(),
									0))));

					Object[] option = { "Dong y", "Thoat" };
					int chon = JOptionPane.showOptionDialog(getParent(),
							"Ban co muon xoa thong tin nay ko?", "Thong bao",
							JOptionPane.YES_NO_OPTION,
							JOptionPane.QUESTION_MESSAGE, null, option,
							option[option.length - 1]);
					if(chon == 0){
						boolean bResult = false;
						try {
							bResult = objcontroller.ExecOfDeleteElement(m_objEntity);
						} catch (Exception e2) {
							// TODO: handle exception
							JOptionPane.showMessageDialog(getParent(), "Loi: "+e2.getMessage(), "Loi", JOptionPane.ERROR);
						}
						
						if(bResult){
							JOptionPane.showMessageDialog(getParent(), "Xoa thanh cong", "thong Bao", JOptionPane.INFORMATION_MESSAGE);
						}else{
							JOptionPane.showMessageDialog(getParent(), "Xoa that bai", "Loi", JOptionPane.ERROR);
						}
					}
				}else{
					JOptionPane.showMessageDialog(getParent(),
							"Ban chon ban ghi de xoa", "ERROR",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnXa.setIcon(new ImageIcon(DONTHUOC.class
				.getResource("/images/6_3.png")));
		btnXa.setFont(new Font("Cambria", Font.PLAIN, 14));
		toolBar.add(btnXa);

		JButton btnCpNht = new JButton("CẬP NHẬT");
		btnCpNht.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (bFlag) {
					m_objEntity.setbFlag(bFlag);
					m_objEntity.setThuocID(Integer.parseInt(String
							.valueOf(objDTM.getValueAt(table.getSelectedRow(),
									0))));
					m_objEntity.setCTBA_ID(Integer.parseInt(String
							.valueOf(objDTM.getValueAt(table.getSelectedRow(),
									1))));
					m_objEntity.setSoLuong(Integer.parseInt(String
							.valueOf(objDTM.getValueAt(table.getSelectedRow(),
									2))));
					m_objEntity.setCachDung(String.valueOf(objDTM.getValueAt(
							table.getSelectedRow(), 3)));

					THEMSUADONTHUOC frm = new THEMSUADONTHUOC(m_objConnection,
							m_objSecurityContext, m_objEntity);
					frm.setVisible(true);

					bFlag = false;
				} else {
					JOptionPane.showMessageDialog(getParent(),
							"Ban chon ban ghi can cap nhat", "ERROR",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnCpNht.setIcon(new ImageIcon(DONTHUOC.class
				.getResource("/images/12.png")));
		btnCpNht.setFont(new Font("Cambria", Font.PLAIN, 14));
		toolBar.add(btnCpNht);

		JButton btnLmTi = new JButton("LÀM TƯƠI");
		btnLmTi.setIcon(new ImageIcon(DONTHUOC.class
				.getResource("/images/2_3.png")));
		btnLmTi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				LoadData(true);
			}
		});
		toolBar.add(btnLmTi);
		btnLmTi.setFont(new Font("Cambria", Font.PLAIN, 14));

		JButton btnThot = new JButton("THOÁT");
		btnThot.setIcon(new ImageIcon(DONTHUOC.class
				.getResource("/images/cancel.gif")));
		btnThot.setFont(new Font("Cambria", Font.PLAIN, 14));
		toolBar.add(btnThot);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 62, 645, 305);
		contentPane.add(scrollPane);

		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				bFlag = true;
			}
		});
		table.setFillsViewportHeight(true);
		table.setCellSelectionEnabled(true);
		table.setColumnSelectionAllowed(true);
		scrollPane.setViewportView(table);
		LoadData(true);
	}

	public void LoadData(boolean typeData) {
		try {
			// 1. Kiểm tra trạng thái kết nối
			if (!m_objConnection.isClosed()) {
				ResultSet objRS = null;
				if (typeData) {
					// 2. Thực thi nhận v�? tập bản ghi Users
					if (objcontroller == null) {
						objcontroller = new DonThuocConTrollers(
								m_objConnection, m_objSecurityContext);
					}
					try {
						objRS = objcontroller.ExecOfGetElements();
					} catch (Exception e) {
						JOptionPane.showMessageDialog(getParent(), "Err: " + e,
								"Thông báo", JOptionPane.ERROR_MESSAGE);
					}
				}
				// 3. Khai báo khởi tạo đối tượng
				Controls objControls = new Controls();

				// 4. Thực thi phương thức getModels để nhận v�?
				// DefaulTableModels
				try {
					objDTM = objControls.getModels(objRS);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				table.setModel(objDTM);

			} else {
				JOptionPane.showMessageDialog(getParent(),
						"Không kết nối được với CSDL!", "Thông báo",
						JOptionPane.ERROR_MESSAGE);
			}
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(getParent(),
					"Có lỗi xảy ra. Chi tiết: " + ex.getMessage(), "Thông báo",
					JOptionPane.ERROR_MESSAGE);
		}
	}
}
